import request, { ApiResponse } from './request'

// 通知数据类型（后台管理）
export interface NotificationData {
  id: string
  title: string
  content: string
  type: 'info' | 'warn' | 'error' | 'success'
  status: number
  push_count?: number // 推送用户数（后台管理）
  created_at: string
  updated_at: string
}

// 用户通知数据类型（前台用户）
export interface UserNotificationData {
  id: string
  title: string
  content: string
  type: 'info' | 'warn' | 'error' | 'success'
  is_read: boolean
  pushed_at: string
  created_at: string
  updated_at: string
}

// 创建通知参数
export interface CreateNotificationParams {
  title: string
  content: string
  type?: 'info' | 'warn' | 'error' | 'success'
}

// 更新通知参数
export interface UpdateNotificationParams {
  id: string
  title?: string
  content?: string
  type?: 'info' | 'warn' | 'error' | 'success'
  status?: number
}

// 删除通知参数
export interface DeleteNotificationParams {
  id: string
}

// 通知列表查询参数（后台管理）
export interface NotificationListParams {
  page?: number
  pageSize?: number
  type?: 'info' | 'warn' | 'error' | 'success'
  status?: number
}

// 用户通知列表查询参数（前台用户）
export interface UserNotificationListParams {
  page?: number
  pageSize?: number
  type?: 'info' | 'warn' | 'error' | 'success'
  unread?: boolean
}

// 通知列表响应
export interface NotificationListResponse {
  list: NotificationData[]
  total: number
  page: number
  pageSize: number
}

// 通知详情参数
export interface NotificationDetailParams {
  id: string
}

// 标记已读参数
export interface MarkReadParams {
  id: string
}

// 标记已读响应
export interface MarkReadResponse {
  message: string
}

// 标记全部已读响应
export interface MarkReadAllResponse {
  message: string
}

// 推送通知参数
export interface PushNotificationParams {
  id: string
  user_ids?: string[]
}

// 推送通知响应
export interface PushNotificationResponse {
  message: string
  total_users: number
  new_pushes: number
  already_pushed: number
}

// 用户通知列表响应
export interface UserNotificationListResponse {
  list: UserNotificationData[]
  total: number
  page: number
  pageSize: number
}

// 创建通知（仅管理员）
export const createNotification = (data: CreateNotificationParams): Promise<ApiResponse<NotificationData>> => {
  return request.post<NotificationData>('/notifications/create', data)
}

// 更新通知（仅管理员）
export const updateNotification = (data: UpdateNotificationParams): Promise<ApiResponse<NotificationData>> => {
  return request.post<NotificationData>('/notifications/update', data)
}

// 删除通知（仅管理员）
export const deleteNotification = (data: DeleteNotificationParams): Promise<ApiResponse<{ message: string }>> => {
  return request.post<{ message: string }>('/notifications/delete', data)
}

// 查询通知列表
export const getNotificationList = (data?: NotificationListParams): Promise<ApiResponse<NotificationListResponse>> => {
  return request.post<NotificationListResponse>('/notifications/list', data || {})
}

// 查询通知详情
export const getNotificationDetail = (data: NotificationDetailParams): Promise<ApiResponse<NotificationData>> => {
  return request.post<NotificationData>('/notifications/detail', data)
}

// 标记通知为已读
export const markNotificationRead = (data: MarkReadParams): Promise<ApiResponse<MarkReadResponse>> => {
  return request.post<MarkReadResponse>('/notifications/read', data)
}

// 标记所有通知为已读
export const markAllNotificationsRead = (): Promise<ApiResponse<MarkReadAllResponse>> => {
  return request.post<MarkReadAllResponse>('/notifications/read-all', {})
}

// 推送通知（仅管理员）
export const pushNotification = (data: PushNotificationParams): Promise<ApiResponse<PushNotificationResponse>> => {
  return request.post<PushNotificationResponse>('/notifications/push', data)
}

// 查询我的通知列表（前台用户）
export const getMyNotificationList = (data?: UserNotificationListParams): Promise<ApiResponse<UserNotificationListResponse>> => {
  return request.post<UserNotificationListResponse>('/notifications/my-list', data || {})
}

// 查询我的通知详情（前台用户）
export const getMyNotificationDetail = (data: NotificationDetailParams): Promise<ApiResponse<UserNotificationData>> => {
  return request.post<UserNotificationData>('/notifications/my-detail', data)
}

